package cn.fuzongyao.learning.android.crime15;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.util.List;

public class CrimePagerActivity extends AppCompatActivity {
    private static final String EXTRA_CRIME_ID = "crime_id";

    public static Intent newIntent(Context context, String id) {
        return new Intent(context, CrimePagerActivity.class).putExtra(EXTRA_CRIME_ID, id);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crime_pager);

        ViewPager viewPager = findViewById(R.id.activity_crime_pager);
        List<Crime> crimes = CrimeLab.getInstance(this).getCrimes();
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return CrimeFragment.newInstance(crimes.get(position).getId());
            }

            @Override
            public int getCount() {
                return crimes.size();
            }
        });

        String crimeId = getIntent().getStringExtra(EXTRA_CRIME_ID);
        viewPager.setCurrentItem(CrimeLab.indexOf(crimes, crimeId));
    }
}
